#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
GitLab数据上传主入口：采集GitLab项目、提交、成员等数据并上传到MySQL数据库
"""
from services.gitlab_service import GitLabService
from services.db_service import DBService
from utils.logger import sync_logger
from config.db_config import GITLAB_DB_CONFIG
import time
from concurrent.futures import ThreadPoolExecutor, as_completed

def sync_project_commits(gitlab_service, db_service, project):
    """同步单个项目的提交数据"""
    project_id = project['id']
    project_name = project['name']
    
    try:
        # 获取项目下的所有提交
        commits = gitlab_service.get_project_commits(project_id)
        if commits:
            # 获取已存在的提交ID
            existing_commits = db_service.get_existing_commits(project_id)
            # 上传提交数据
            db_service.save_commits_batch(commits, project_id)
        return True
    except Exception as e:
        sync_logger.log_error(f"同步项目 {project_name}(ID: {project_id})的提交失败: {e}")
        return False

def main():
    """GitLab数据上传主函数"""
    start_time = time.time()
    sync_logger.start_total_timer()
    
    try:
        # 初始化服务
        gitlab_service = GitLabService()
        # 使用GitLab专用数据库
        db_service = DBService(GITLAB_DB_CONFIG)
        
        # 获取所有项目
        projects = gitlab_service.get_all_projects()
        sync_logger.log_info(f"共获取到 {len(projects)} 个项目")
        
        # 保存项目信息
        db_service.save_projects_batch(projects)
        
        # 并发同步每个项目的提交
        with ThreadPoolExecutor(max_workers=5) as executor:
            futures = {executor.submit(sync_project_commits, gitlab_service, db_service, project): project for project in projects}
            
            for future in as_completed(futures):
                project = futures[future]
                try:
                    future.result()
                except Exception as e:
                    sync_logger.log_error(f"处理项目 {project['name']}(ID: {project['id']}) 时发生异常: {e}")
        
        # 同步所有成员信息
        sync_logger.log_info("开始同步成员信息...")
        from scripts.gitlab_members import sync_all_members
        sync_all_members()
        
        # 同步开发者统计数据
        sync_logger.log_info("开始同步开发者统计数据...")
        from scripts.gitlab_members import DeveloperService
        developer_service = DeveloperService()
        developer_service.sync_developer_statistics()
        
        total_time = time.time() - start_time
        sync_logger.log_info(f"GitLab数据上传完成，总耗时: {total_time:.2f}秒")
    except Exception as e:
        sync_logger.log_error(f"GitLab数据上传失败: {e}")
        total_time = time.time() - start_time
        sync_logger.log_info(f"GitLab数据上传异常退出，总耗时: {total_time:.2f}秒")
    finally:
        sync_logger.stop_total_timer()

if __name__ == "__main__":
    main() 